package wiiWired.controller.console.wizard;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import wiiWired.controller.main.Main;

public class MoteConnectionManager extends JDialog
{
	private static final long serialVersionUID = 1L;

	private static WizardModel model = null;

	public MoteConnectionManager(JFrame frame)
	{
		super(frame, true);

		this.setTitle("Mote Connection Manager");
		this.setSize(765, 503);
		JTable motes = new JTable(model = new WizardModel());
		this.add(new JScrollPane(motes), BorderLayout.CENTER);

		JPanel buttons = new JPanel();

		final JCheckBox connections = new JCheckBox("Allow New Connections");
		connections.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				Main.setAllowNewConnections(connections.isSelected());
			}
		});
		connections.setSelected(Main.isAllowNewConnections());

		buttons.add(connections);

		this.add(buttons, BorderLayout.SOUTH);
		
		this.addWindowListener(new WindowListener()
		{
			@Override
			public void windowClosing(WindowEvent e)
			{
				model = null;
			}
			
			@Override
			public void windowOpened(WindowEvent e)
			{
			}
			
			@Override
			public void windowIconified(WindowEvent e)
			{
			}
			
			@Override
			public void windowDeiconified(WindowEvent e)
			{
			}
			
			@Override
			public void windowDeactivated(WindowEvent e)
			{
			}
			
			@Override
			public void windowClosed(WindowEvent e)
			{
			}
			
			@Override
			public void windowActivated(WindowEvent e)
			{
			}
		});

		this.setVisible(true);
	}

	public static void fireUpdateRequired()
	{
		if (model != null)
			model.fireTableDataChanged();
	}

}
